package com.bjpowernode.task;

import com.bjpowernode.DlykServerApplication;
import com.bjpowernode.constant.DicEnum;
import com.bjpowernode.entity.TDicType;
import com.bjpowernode.mapper.TDicTypeMapper;
import com.bjpowernode.service.DicTypeService;
import com.bjpowernode.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.math3.stat.descriptive.summary.Product;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 读取数据字典中的数据存入缓存中
 */
@Component
@EnableScheduling //开启定时任务
@RequiredArgsConstructor
public class DataTask {

    private final DicTypeService dicTypeService;

    private final ProductService productService;

    //5秒执行一次
//    @Scheduled(cron = "0/5 * * * * ?")
    public void loadDic(){
        //从数据库中查询数据字典
        //查询数据字典 查询一type对多value的数据
        List<TDicType> tDicTypeList = dicTypeService.getDicTypes();
        for (TDicType tDicType : tDicTypeList) {
            /*
                key:sex value:男 女
                key:appellation value:先生 女士
             */
            //将数据字典数据存入缓存中
            DlykServerApplication.cacheMap.put(tDicType.getTypeCode(),tDicType.getDicValueList());
        }

        //查询产品数据
        List<Product> productList = productService.getProducts();

        //将数据字典数据存入缓存中
        DlykServerApplication.cacheMap.put(DicEnum.INTENTION_PRODUCT.getCode(), productList);


    }
}
